package sqlnode.impl;

/*
* @auther 顶风少年 
* @mail dfsn19970313@foxmail.com
* @date 2020-01-04 18:10
* @notify
   <if test="username!=null and username!=''">
            AND username like '%${username}'
            <if test="password!=null and password!=''">
                AND password = #{password}
            </if>
    </if>
    包含了 test条件还有其子节点，所以内部有SqlNode
* @version 1.0
*/

import mapping.DynamicContext;
import sqlnode.SqlNode;
import utils.OgnlUtils;

public class IfSqlNode implements SqlNode {
    //if语句的表达式
    private String test;

    //if语句成立后的体，可能是一个TextSqlNode,也可能是一个MixedSqlNode
    private SqlNode rootSqlNode;


    public IfSqlNode(String test, SqlNode rootSqlNode) {
        this.test = test;
        this.rootSqlNode = rootSqlNode;
    }

    @Override
    public void apply(DynamicContext context) throws Exception {
        //使用ognl表达式，判断test是否正确
        boolean b = OgnlUtils.evaluateBoolean(test, context.getParam());
        //此时如果是MixedSqlNode那则会执行MixedSqlNode的apply
        if (b) {
            //执行sqlNode自己的apply
            rootSqlNode.apply(context);
        }
    }
}
